package com.sgl.gk.tools;

import cn.hutool.json.JSONUtil;
import com.sg.comm.common.Ret;
import com.sg.comm.opcuav2.NodeDTOV2;
import com.sg.comm.opcuav2.OpcUaReadReqV2;
import com.sg.comm.opcuav2.OpcUaReqV2;
import com.sg.comm.opcuav2.OpcUaWriteReqV2;
import com.sg.comm.opcuav2.batch.BatchReadDTO;
import com.sg.comm.opcuav2.single.ReadByNodeReq;
import com.sg.comm.service.OpcUaServiceV2;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author:xec
 * @date:2022/12/30 13:09
 **/
public class OpcUATool {


    public static Boolean isConnect(OpcUaServiceV2 opcUaServiceV2,OpcUaWriteReqV2 opcUaWriteReqV2){
        try {
            OpcUaReadReqV2 opcUaReadReqV2 = new OpcUaReadReqV2();
            opcUaReadReqV2.setUid(opcUaWriteReqV2.getUid());
            Ret<BatchReadDTO> batchReadDTORet = opcUaServiceV2.isConn(opcUaReadReqV2);
            if (!batchReadDTORet.getData().getIsConn()){
                return Boolean.FALSE;
            }else {
                return Boolean.TRUE;
            }
        } catch (Exception e) {
            return Boolean.FALSE;
        }
    }

    public static OpcUaWriteReqV2 valueIsChange(OpcUaServiceV2 opcUaServiceV2, OpcUaWriteReqV2 opcUaWriteReqV2) {
//        OpcUaReadReqV2 opcUaReadReqV2 = new OpcUaReadReqV2();
//        opcUaReadReqV2.setUid(opcUaWriteReqV2.getUid());
//        Ret<BatchReadDTO> batchReadDTORet = opcUaServiceV2.readData(opcUaReadReqV2);
        ReadByNodeReq readByNodeReq = new ReadByNodeReq();
        readByNodeReq.setUid(opcUaWriteReqV2.getUid());
        List<NodeDTOV2> writeNodeList1 = opcUaWriteReqV2.getWriteNodeList();
        readByNodeReq.setNodeDTOV2List(writeNodeList1);
        Ret<BatchReadDTO> batchReadDTORet = opcUaServiceV2.readDataByNodeList(readByNodeReq);
        BatchReadDTO data = batchReadDTORet.getData();
        List<NodeDTOV2> dtoList = data.getDtoList();
        List<NodeDTOV2> writeNodeList = opcUaWriteReqV2.getWriteNodeList();
        List<NodeDTOV2> newNodeDto = new ArrayList<>();
        for (NodeDTOV2 nodeDTOV2 : writeNodeList) {
            if (nodeDTOV2.getNodeValue()!=null) {
                for (NodeDTOV2 dtov2 : dtoList) {
                    if (nodeDTOV2.getNodeName().equals(dtov2.getNodeName())||nodeDTOV2.getNodeValue().equals(dtov2.getDisplayName())) {
                        String value = new String();
                        int i = nodeDTOV2.getNodeValue().toString().replace("}", "").indexOf("=");
                        if (i > 0) {
                            value = nodeDTOV2.getNodeValue().toString().replace("}", "").substring(i + 1);
                        } else {
                            value = nodeDTOV2.getNodeValue().toString();
                        }
                        String value2 = new String();
                        i = dtov2.getNodeValue().toString().replace("}", "").indexOf("=");
                        if (i > 0) {
                            value2 = dtov2.getNodeValue().toString().replace("}", "").substring(i + 1);
                        } else {
                            value2 = dtov2.getNodeValue().toString();
                        }
                        if (!StringUtils.isEmpty(value)&&!value.equals(value2)) {
                            newNodeDto.add(nodeDTOV2);
                            System.out.println("真实设置属性：" + JSONUtil.toJsonStr(nodeDTOV2));
                            break;
                        }
                    }
                }
            }
        }
        opcUaWriteReqV2.setWriteNodeList(newNodeDto);
        return opcUaWriteReqV2;
    }

    /**
     *
     */
    public static String getValue(NodeDTOV2 datum){
        String value = new String();
        if (datum.getNodeValue()!=null) {
            int i = datum.getNodeValue().toString().replace("}", "").indexOf("=");
            if (i > 0) {
                value = datum.getNodeValue().toString().replace("}", "").substring(i + 1);
            } else {
                value = datum.getNodeValue().toString();
            }
        }
        return value;
    }


}
